﻿using System;
using System.Drawing;
using System.IO;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer.Enums;

namespace Yglu.WebAdmin
{
	public class BasePage : Page
	{
		#region Properties

		protected String LogMessage { get; set; }

		#endregion

		#region Main methods

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			if (SessionManager.LoggedUser == null && !Path.GetFileName(Request.Path).ToLower().Contains("login.aspx"))
			{
				Response.Redirect("/Login.aspx");
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			Title = Settings.SystemConfig.CompanyName + " - " + GetLocalResourceObject("PageTitle");
		}

		protected override void OnPreRenderComplete(EventArgs e)
		{
			base.OnPreRenderComplete(e);

			if (IsPostBack && SessionManager.LastLogMessage != LogMessage)
			{
				LogManager.Insert(LogMessage);
			}
		}

		protected void CheckPermissions(AdminLevel minLevel)
		{
			if (SessionManager.LoggedUser.AdminLevel < minLevel)
			{
				Response.Redirect("Login.aspx");
			}
		}

		#endregion

		#region Caching

		protected Object GetCache(String key)
		{
			return Cache.Get(key);
		}

		protected void SetCache(String key, Object value, CacheDuration duration)
		{
			Cache.Add(key, value, null, DateTime.Now.AddSeconds((Int32)duration), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
		}

		#endregion

		#region GridView defaults

		protected void SetDefaults(GridView grid)
		{
			grid.AllowPaging = false;
			grid.AllowSorting = false;
			grid.AutoGenerateColumns = false;
			grid.CssClass = "GridView";
			grid.EmptyDataText = Messages.GridNoDataText;
			grid.EnableSortingAndPagingCallbacks = true;

			foreach (DataControlField col in grid.Columns)
			{
				if (col.AccessibleHeaderText == "Edit")
				{
					col.HeaderText = Messages.View;
					((ButtonField)col).Text = Messages.GridViewEditText;
				}
			}
		}

		protected void SetDefaults(Chart chart)
		{
			chart.BackColor = Color.LightYellow;
			chart.BackGradientStyle = GradientStyle.TopBottom;
			chart.BorderColor = Color.Black; 
			chart.BorderSkin.SkinStyle = BorderSkinStyle.FrameThin1;
			chart.BorderWidth = 1;
			chart.ImageType = ChartImageType.Png;
			chart.Palette = ChartColorPalette.BrightPastel;

			chart.ChartAreas[0].BorderWidth = 1;
			chart.ChartAreas[0].BorderColor = Color.DarkGray;

			chart.Legends.Add(new Legend());
			chart.Legends[0].Alignment = StringAlignment.Center;
			chart.Legends[0].BackColor = Color.Transparent;
			chart.Legends[0].Docking = Docking.Bottom;
			chart.Legends[0].Enabled = true;
			chart.Legends[0].LegendStyle = LegendStyle.Row;
			chart.Legends[0].Font = new Font("Arial", 9, FontStyle.Bold);
			
			foreach (Series s in chart.Series)
			{
				s.BorderWidth = 2;
			}

			foreach (Title t in chart.Titles)
			{
				t.Font = new Font("Arial", 13, FontStyle.Bold);
			}
		}

		#endregion
	}
}